package cn.school;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

import cn.school.util.DBUtil;

public class GetStuPage2 {

	public static void main(String[] args) {
		// 不支持limit这样的分页方式
		Scanner in = new Scanner(System.in);
		System.out.println("请输入页码和条数:");
		int pageNo = in.nextInt();
		int pageSize = in.nextInt();
		int offset = (pageNo - 1) * pageSize;
		offset = Math.max(offset, 0);// 不能小于0
		String sql = "select * from s_student order by stuno asc ";
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBUtil.getConnection();
			ps = con.prepareStatement(sql);
			ps.setMaxRows(offset + pageSize);// !
			rs = ps.executeQuery();
			if (offset > 0)
				rs.absolute(offset);
			while (rs.next()) {
				System.out.println(rs.getLong("stuno") + "," + rs.getString("sname"));

			}

			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
